from langchain.tools import BaseTool
from pydantic import BaseModel, Field, ValidationError
from typing import Optional, Type
from tools.log_handler import logger
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper

# 定义输入模型
class WebSearchInput(BaseModel):
    query: str = Field(..., description="搜索查询内容")

# 自定义网络搜索工具（修复字段覆盖问题）
class WebSearchTool(BaseTool):
    name: str = "web_search"
    description: str = "访问互联网获取实时信息，适用于需要最新数据的查询"
    args_schema: Type[BaseModel] = WebSearchInput

    def _run(self, query: str) -> str:
        """执行网络搜索并返回结果"""
        search = DuckDuckGoSearchAPIWrapper()
        return search.run(query)